<%@ page import="java.util.*"%>
<%@ page import="objects.*"%>
<%@ page import="Domain.UserClient.ButtonsFunctions"%>
<%@page import="Domain.DataManager"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>

<%
	DataManager dm = (DataManager) (session.getAttribute("dm"));
	ButtonsFunctions bf = (ButtonsFunctions) (session
			.getAttribute("bf"));

	if (dm == null || dm.getUserStatus() == DataManager.DISCONNECTED) {
		response.sendRedirect("Welcome.jsp");
	} else {
%>

<%
		String nameOfThread = request.getParameter("nameOfThread");
		dm.setCurrThread(nameOfThread);

		String nameOfForum = dm.getCurrForum();
		nameOfThread = dm.getCurrThread();
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" href="style.css" type="text/css">

<title>Choose Post</title>

<link rel="stylesheet" href="windowfiles/dhtmlwindow.css" type="text/css" />

<script type="text/javascript" src="windowfiles/dhtmlwindow.js"></script>

<script language="javascript" type="text/javascript">
		
		//Browser Support Code
		function updateUsingAjax() {
			var ajaxRequest;

			// The variable that makes Ajax possible! 
			try {
				// Opera 8.0+, Firefox, Safari 
				ajaxRequest = new XMLHttpRequest();
			} catch (e) {
				// Internet Explorer Browsers 
				try {
					ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try {
						ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e) {
						// Something went wrong 
						alert("Your browser broke!");
						return false;
					}
				}
			}
			
			// Create a function that will receive data sent from the server 
			ajaxRequest.onreadystatechange = function() {
				if (ajaxRequest.readyState == 4) {
					var response = ajaxRequest.responseText;			
					if(response.trim() == "")
					{
						alert("You have been disconnected from the server!");
						parent.location = 'Welcome.jsp';
					}
					else
						document.getElementById("numOfNotifications").innerHTML = response;
				}
			};

			var url = "GetNumOfNotifications.jsp";
			var params = "";
			ajaxRequest.open("POST", url, true);

			//Send the proper header information along with the request
			ajaxRequest.setRequestHeader("Content-type",
					"application/x-www-form-urlencoded");
			ajaxRequest.setRequestHeader("Content-length", params.length);
			ajaxRequest.setRequestHeader("Connection", "close");

			ajaxRequest.send(params);
		}

		function updateNumOfNotifications() {
			updateUsingAjax();
			setTimeout("updateNumOfNotifications()", 3000);
		}
		
		function openmypage() { //Define arbitrary function to run desired DHTML Window widget codes
			ajaxwin = dhtmlwindow.open("ajaxbox", "ajax", "GetNotification.jsp", 
					"Notification window", "width=450px,height=300px,left=300px,top=100px,resize=1,scrolling=1");
			ajaxwin.onclose = function() {
				return window.confirm("Close notification window?");
			}
		}
		
	</script>


</head>
<body>

	<table width="100%" height="100%" border="1" cellspacing="0"
		cellpadding="0">

		<!-- HEADER -->
		<tr class="header">
			<td>
				<!-- cool navigation toolbar code -->
				<ul id="navlist">
					<li id="logout"><a href="Logout.jsp"></a>
					</li>
					<li id="prev"><a href="ChooseThread.jsp?nameOfForum=<%out.print(dm.getCurrForum());%>"></a>
					</li>
					<li id="notifications"><a href="#" onClick="openmypage(); return false"></a>
					</li>
					<li id="notificationCounter"><center><font size=3 id="numOfNotifications"></font></center></li>
					
					<li id="currentForum">Current Forum is: <b><i><font
								color='#FF0000'> <%
					 	out.print(dm.getCurrForum());
					 %> </font> </i> </b>
										</li>
										<li id="currentThread"><br>Current Thread is: <b><i><font
													color='#FF0000'> <%
					 	out.print(dm.getCurrThread());
					 %> </font> </i> </b>
					</li>
					<li id="connection">Connected as: <b><i><font
								color='#FF0000'> <%
 	out.print(dm.getCurrUser());
 %> </font> </i> </b>
					</li>
				</ul></td>
		</tr>

		<!-- BODY -->
		<tr class="body">
			<td class="forumBody">

				<center>
					<h6>
						<font color="#FFF11" size="7"><i>Please choose a Post</i>
						</font>
					</h6>
				</center>

				<center>
					<%
						//-----------------show the relevant buttons-------------------
								
								 out.print("<button type=\"button\" onClick=\"parent.location = 'AddPost.jsp';\">Add Post</button>");
								/*
								 bf.processGetTypeOfUser();
								 int userType = dm.getTypeOfUser();

								 if (userType == 1) {
								 out.print("<button type=\"button\" onClick=\"parent.location = 'ManageUsers.jsp';\">Manage Users</button>");
								 out.print("<button type=\"button\" onClick=\"parent.location = 'AddForum.jsp';\">Add Forum</button>");
								 }
								 */
					%>
				</center> <br> 
				
				
				<%
 	//-----------------Print the list of messages-------------------
 	
 			bf.processGetMessages(nameOfThread);

 			Vector<Message> messages = dm.getVectorMessages();
 			if (messages == null)
 				System.out.println("vector of threads didn't arrive!");
 			else {
 				if (messages.size() > 0) {
 					for (int i = 0; i < messages.size(); i++) {
 						Message m = messages.elementAt(i);
 						String t = m.getTitle();
 						String b = m.getMessage();
 						String o = m.getOwner().getUserName();
 						Date d = m.getDate();

 						
 						//-----------------printing the message frame------------------
 						out.print("" + "<div class=\"messageboard\">"
 								+ "<div class=\"subject\">" + t);

 						//-----------------show the relevant buttons-------------------

 						if (dm.getCurrTypeOfUser() == 1
 								| o.equals(dm.getCurrUser()))
 							out.print("<input " + "type=\"button\" "
 									+ "class=\"DeleteMessageButton\" "
 									+ "value=\"Delete\" " + "onClick=\"parent.location = 'DeletePost.jsp?title="+ t +"';\">");

 						if (o.equals(dm.getCurrUser()))
 							out.print("<input " + "type=\"button\" "
 									+ "class=\"EditMessageButton\" "
 									+ "value=\"Edit\" " + "onClick=\"parent.location = 'EditPost.jsp?title="+ t + "&body=" + b + "';\">");

 						//----------------finished printing buttons-------------------
 					
 						int day = d.getDate();
 						int month = d.getMonth()+1;
 						int year = d.getYear()+1900;
 						int hour = d.getHours();
 						int minutes = d.getMinutes();
 						
 						out.print("</div>" + "<div class=\"body\">" + b
 								+ "</div>"
 								+ "<div class=\"date\"> published by "
 								+ "<b><i><font color='#FF0000'> " + o + " </font></i></b>in " + day +'/'+ month + '/' + year + "  " + hour +":" + minutes + " </div>" + "</div>");
 					}

 				} else {
 					out.println("<b><font color = \"#FFAA44\"> <div align=\"center\">there are no messages to show!</center></div></font></b>");
 				}
 			}
 %>
			</td>
		</tr>
	</table>
	<script language="javascript" type="text/javascript">updateNumOfNotifications();</script>
</body>
</html>
<%
	}
%>